local skel = fk.CreateSkill {
  name = "emo__chuaisi",
}

Fk:loadTranslationTable{
  ["emo__chuaisi"] = "揣思",
  [":emo__chuaisi"] = "回合开始时，或出牌阶段限一次，你可以重铸任意张牌，若重铸了本回合未以此法重铸的花色，下个阶段改为出牌阶段。",

  ["@emo__chuaisi-turn"] = "揣思",
  ["#emo__chuaisi"] = "揣思:你可以重铸任意张牌",
  ["#EmoChuaisiChange"] = "%from 的下个阶段 %arg 改为 %arg2",
}

skel:addEffect("active", {
  anim_type = "drawcard",
  min_card_num = 1,
  target_num = 0,
  card_filter = Util.TrueFunc,
  times = function (self, player)
    return 1 - player:usedEffectTimes(self.name, Player.HistoryPhase)
  end,
  can_use = function (self, player)
    return player:usedEffectTimes(self.name, Player.HistoryPhase) == 0
  end,
  on_use = function (self, room, use)
    local change = false
    local player = use.from
    local mark = player:getTableMark("@emo__chuaisi-turn")
    for _, id in ipairs(use.cards) do
      local suit = Fk:getCardById(id):getSuitString(true)
      if suit ~= "log_nosuit" and table.insertIfNeed(mark, suit) then
        change = true
      end
    end
    room:setPlayerMark(player, "@emo__chuaisi-turn", mark)
    room:recastCard(use.cards, player, skel.name)
    if not change then return end
    local turn = room.logic:getCurrentEvent():findParent(GameEvent.Turn, true)
    if turn == nil then return end
    local tdata = turn.data
    if #tdata.phase_table > tdata.phase_index then
      local old = tdata.phase_table[tdata.phase_index + 1].phase
      tdata.phase_table[tdata.phase_index + 1].phase = Player.Play
      room:sendLog{ type = "#EmoChuaisiChange", from = player.id, arg = Util.PhaseStrMapper(old), arg2 = "phase_play", toast = true }
    end
  end,
})

skel:addEffect(fk.TurnStart, {
  anim_type = "drawcard",
  on_cost = function (self, event, target, player, data)
    local _ , dat = player.room:askToUseActiveSkill(player, {skill_name = skel.name, skip = true, prompt = "#emo__chuaisi" })
    if dat then
      event:setCostData(self, { tos = dat.targets, cards = dat.cards, from = player })
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local skill = Fk.skills[skel.name] ---@type ActiveSkill
    if skill then
      skill:onUse(player.room, event:getCostData(self))
    end
  end,
})

return skel
